package com.yidiandao.business.ui.main.shoppingcart.confirmorder;

import android.app.Activity;
import android.content.Intent;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseActivity;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.model.ConfirmOrderModel;
import com.yidiandao.business.utils.ActivityUtils;

/**
 * 新的确认订单页面Activity.
 */
public class ConfirmOrderActivity extends BaseActivity {

    private ConfirmOrderFragment mConfirmOrderFragment;

    /**
     * 非活动商品的结算
     * <pre>
     *     1. 来自购物车 fromShoppingCart = true
     *     2. 非购物车商品 fromShoppingCart = false
     * </pre>
     *
     * @param fromShoppingCart 是否来自购物车
     */
    public static void actionStart(Activity activity, boolean fromShoppingCart) {
        Intent intent = new Intent(activity, ConfirmOrderActivity.class);
        intent.putExtra(Intents.EXTRA_FROM_SHOPPING_CART, fromShoppingCart);
        activity.startActivity(intent);
    }

    /**
     * 首页活动商品的跳转到确认订单结算
     *
     * @param goodsType 商品的类型
     * @see Goods.TYPE
     */
    public static void actionStartForActiveGoods(Activity activity, String goodsId, int goodsNumber, int goodsType) {
        Intent intent = new Intent(activity, ConfirmOrderActivity.class);
        intent.putExtra(Intents.EXTRA_FROM_SHOPPING_CART, false);
        intent.putExtra(Intents.HOME_GOODS_ID, goodsId);
        intent.putExtra(Intents.HOME_GOODS_NUMBER, goodsNumber);
        intent.putExtra(Intents.EXTRA_DIRECT, true);
        intent.putExtra(Intents.GOODS_TYPE, goodsType);
        activity.startActivity(intent);
    }

    @Override
    protected void initFragment() {
        mConfirmOrderFragment = (ConfirmOrderFragment) getSupportFragmentManager().findFragmentById(R.id.container_confirm_order);
        if (mConfirmOrderFragment == null) {
            mConfirmOrderFragment = ConfirmOrderFragment.getInstance(getIntent().getExtras());
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), mConfirmOrderFragment, R.id.container_confirm_order);
        }
    }

    @Override
    protected void initPresenter() {
        new ConfirmOrderPresenter(mConfirmOrderFragment, new ConfirmOrderModel());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_confirm_order;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mConfirmOrderFragment.onBackPressed();
    }
}
